package com.toolenaar.socialiceThis;

import java.io.InputStream;
import java.net.URL;
import java.util.EmptyStackException;
import java.util.List;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class FacebookFriendsAdapter extends BaseAdapter {

	private Context _context;
	private List<FacebookFriends> _friends;
	
	public FacebookFriendsAdapter(Context c, List<FacebookFriends> friends)
	{
		_context = c;
		_friends = friends;
	}
	
	public int getCount() {
		if(_friends == null)
		{
			return 0;
		}
		return _friends.size();
	}

	public Object getItem(int id) {
		if(_friends == null)
		{
			return null;
		}
		return _friends.get(id);
	}

	public long getItemId(int id) {
		return id;
	}

	public View getView(int id, View view, ViewGroup viewGroup) {
		LinearLayout layout = null;
		FacebookFriends friend;
		if(_friends == null)
		{
			friend = new FacebookFriends("error","error","error");
		}
		else
		{
			friend = _friends.get(id);//gets the current friend
		}
		layout = (LinearLayout)LayoutInflater.from(_context).inflate(R.layout.friendslist, viewGroup, false);//gets the view
		ImageView image = (ImageView)layout.findViewById(R.id.TextView01);
		Drawable drawable = LoadImageFromWebOperations(friend.getPicture());
		image.setImageDrawable(drawable);
		TextView tv = (TextView)layout.findViewById(R.id.TextView02);
		tv.setText(friend.getName());
		return layout;
	}
	
   private Drawable LoadImageFromWebOperations(String url)
   {
		try
		{
			InputStream is = (InputStream) new URL(url).getContent();
			Drawable d = Drawable.createFromStream(is, "src name");
			return d;
		}catch (Exception e) {
			System.out.println("Exc="+e);
			return null;
		}
	}
}
